<template>
  <div>
    <pageHeader :title="title" :breadCrumb="breadCrumb" :show-back="true">
      <template #buts>
        <a-space :size="30">
          <a-button type="outline" @clikc="router.back()">
            <template #default>取消</template>
          </a-button>
          <a-button type="primary" @click="submit">
            <template #default>执行</template>
          </a-button>
        </a-space>
      </template>
    </pageHeader>
    <div>
      <div class="info-item">
        <div style="width: 100%; display: flex; margin: 20px 50px">
          <a-space>
            <div
              style="
                width: 4px;
                height: 15px;
                background-color: rgba(61, 126, 255, 1);
                border: none;
                border-radius: 2px;
              "
            >
            </div>
            <h3>基本信息</h3>
          </a-space>
        </div>
        <div style="margin-left: 10%"
          ><a-form :model="taskData" auto-label-width>
            <a-row :gutter="24">
              <a-col :span="12">
                <a-form-item field="posts.post2" label="收货单号:"
                  ><a-input style="width: 320px" v-model="form.number"
                /></a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item field="posts.post2" label="关联采购订单号：">
                  <a-select
                    :style="{ width: '320px' }"
                    placeholder="Please select ..."
                    @change="getInfo"
                  >
                    <a-option
                      v-for="item in BuyOrderList"
                      :key="item.id"
                      :value="item.id"
                      >{{ item.number }}</a-option
                    >
                  </a-select>
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item field="posts.post2" label="供应商编码:"
                  ><a-input
                    style="width: 320px"
                    v-model="taskData.form.createByName"
                    placeholder=""
                    allow-clear
                /></a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item field="posts.post2" label="供应商名称："
                  ><a-input
                    :style="{ width: '320px' }"
                    placeholder="Please enter something"
                    v-model="form.supplierName"
                    disabled
                    allow-clear
                /></a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item field="posts.post2" label="采购员：">
                  <a-input
                    :style="{ width: '320px' }"
                    placeholder="Please enter something"
                    v-model="form.purchaseStaffName"
                    disabled
                    allow-clear
                  />
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item field="posts.post2" label="收货日期：">
                  <a-date-picker style="width: 200px" disabled />
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item field="posts.post2" label="收货人：">
                  <a-select
                    v-model="form.entryStaffId"
                    :style="{ width: '320px' }"
                    placeholder="请选择任务负责人"
                    @change="getUserInfo"
                  >
                    <a-option
                      v-for="item in Personne"
                      :key="item.id"
                      :value="item.id"
                      >{{ item.name }}</a-option
                    >
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item field="posts.post2" label="收货人联系电话：">
                  <a-input
                    v-model="form.StaffPhone"
                    :style="{ width: '320px' }"
                    placeholder="Please enter something"
                    disabled
                    allow-clear
                  />
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item field="posts.post2" label="暂存区域：">
                  <a-textarea
                    v-model="form.location"
                    placeholder="Please enter something"
                    allow-clear
                  />
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item field="posts.post2" label="备注：">
                  <a-textarea
                    v-model="form.remark"
                    placeholder="Please enter something"
                    allow-clear
                  />
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <div style="width: 100%; display: flex; margin: 20px 50px">
          <a-space>
            <div
              style="
                width: 4px;
                height: 15px;
                background-color: rgba(61, 126, 255, 1);
                border: none;
                border-radius: 2px;
              "
            >
            </div>
            <h3>质检与入库信息</h3>
          </a-space>
        </div>
        <div style="margin-left: 10%"
          ><a-form :model="taskData.form" auto-label-width>
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item field="posts.post2" label="质检员：">
                  <a-space>
                    <a-tooltip
                      content="注：可以直接选择质检员下发质检任务，如不指定则直接进入“来料质检任务”待检任务中。"
                      position="top"
                    >
                      <icon-exclamation-circle-fill
                        style="font-size: 20px; color: blue"
                      />
                    </a-tooltip>

                    <a-select
                      :style="{ width: '320px' }"
                      v-model="form.inspectionStaffId"
                      @change="getUserInfo"
                    >
                      <a-option
                        v-for="item in Personne"
                        :key="item.id"
                        :value="item.id"
                        >{{ item.name }}</a-option
                      >
                    </a-select>
                  </a-space>
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item field="posts.post2" label="仓管员：">
                  <a-select
                    :style="{ width: '320px' }"
                    v-model="form.warehouseStaffId"
                    @change="getUserInfo"
                  >
                    <a-option
                      v-for="item in Personne"
                      :key="item.id"
                      :value="item.id"
                      >{{ item.name }}</a-option
                    >
                  </a-select></a-form-item
                >
              </a-col>
            </a-row>
          </a-form>
        </div>
        <div style="width: 100%; display: flex; margin: 20px 50px">
          <a-space>
            <div
              style="
                width: 4px;
                height: 15px;
                background-color: rgba(61, 126, 255, 1);
                border: none;
                border-radius: 2px;
              "
            >
            </div>
            <h3>采购明细：</h3>
          </a-space>
        </div>
        <div
          style="
            width: 90%;
            margin-left: 5%;
            box-shadow: 1px 1px 5px rgb(153 153 153 / 35%);
            border-radius: 4px;
          "
        >
          <div style="padding: 30px">
            <div style="text-align: right" class="button-item"></div
            ><tableTurn
              v-if="form.product"
              style="margin-top: 10px"
              :table-data="getProduct"
              ref="tableRef"
              :pagination="false"
              :args="args"
            >
              <a-table-column :width="80" align="center">
                <template #title> 序号 </template>
                <template #cell="{ rowIndex }">
                  <div style="position: relative">
                    <div>{{ rowIndex + 1 }}</div>
                  </div>
                </template>
              </a-table-column>
              <a-table-column
                align="center"
                :width="80"
                title="物料编码"
                data-index="number"
              >
              </a-table-column>
              <a-table-column
                align="center"
                :width="80"
                title="物料名称"
                data-index="name"
              >
              </a-table-column>

              <a-table-column
                align="center"
                :width="80"
                title="规格型号"
                data-index="spec"
              >
              </a-table-column>
              <a-table-column
                align="center"
                title="基本单位"
                data-index="purchaseUnitName"
                :width="80"
              >
              </a-table-column>

              <a-table-column
                align="center"
                :width="80"
                title="订单数量"
                data-index="amount"
              >
              </a-table-column>
              <a-table-column
                align="center"
                :width="80"
                title="已来料数量"
                data-index="amount"
              >
              </a-table-column>
              <a-table-column
                align="center"
                :width="150"
                title="本次来料数量"
                data-index="guidePrice"
              >
                <template #cell="{ record }">
                  <a-input-number
                    :min="1"
                    placeholder="请输入销售数量"
                    v-model="record.purchaseOrderProduct.amount"
                    hide-button
                  />
                </template>
              </a-table-column>
              <a-table-column align="center" :width="150" title="检验类别">
                <template #cell="{ record }">
                  <a-select
                    v-model="record.purchaseOrderProduct.detectionType"
                    placeholder="Please select ..."
                  >
                    <a-option :value="1">免检</a-option>
                    <a-option :value="2">全检</a-option>
                    <a-option :value="3">抽检</a-option>
                  </a-select>
                </template>
              </a-table-column>
              <a-table-column
                align="center"
                :width="80"
                title="优先级"
                data-index="spec"
              >
                <template #cell="{ record }">
                  <a-select
                    v-model="record.purchaseOrderProduct.level"
                    placeholder="Please select ..."
                  >
                    <a-option :value="1">低</a-option>
                    <a-option :value="2">中</a-option>
                    <a-option :value="3">高</a-option>
                    <a-option :value="4">紧急</a-option>
                  </a-select>
                </template>
              </a-table-column>
              <a-table-column
                align="center"
                :width="80"
                title="入库批次"
                data-index="spec"
              >
                <template #cell="{}">
                  <a-input-number
                    :min="1"
                    placeholder="请输入销售数量"
                    hide-button
                /></template>
              </a-table-column> </tableTurn
          ></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import {
    reactive,
    ref,
    computed,
    onMounted,
    defineAsyncComponent,
  } from 'vue';
  import { useRoute, useRouter } from 'vue-router';
  import {
    getPurchaseOrderList,
    getSinglePurchaseOrder,
  } from '@/api/buy-management/buy-order/index';
  import { Message } from '@arco-design/web-vue';
  import {
    GetPersonnelinformation,
    Getsingleperson,
  } from '@/api/home/System settings/personal-information/index';
  import { postWarehouseEntry } from '@/api/inventory-management/warehousing-management/receiving-management/index';
  import { useBuyStore } from '@/store';
  import { cloneDeep } from 'lodash';

  // 获取人员信息
  const Personne = ref();
  const Personnelinformation = async () => {
    const res = await GetPersonnelinformation({ page: 0, size: -1 });
    Personne.value = res.content.data;
  };
  Personnelinformation();

  const orderRespData = ref();
  const store = useBuyStore();
  const SupplierProductCode = ref();
  const isSupplierVisible = ref(false);
  const route = useRoute();
  const router = useRouter();
  const tableRef = ref();
  const title: string = '新建采购入库单';
  const breadCrumb: string[] = [
    '库存管理',
    '入库管理',
    '采购入库',
    '新建采购入库单',
  ];
  const id = Number(route.query.id);

  const form = ref({
    number: undefined,
    purchaseOrderNumber: undefined,
    entryType: undefined,
    supplierId: undefined,
    supplierName: undefined,
    purchaseStaffId: undefined,
    purchaseStaffName: undefined,
    entryStaffId: undefined,
    warehouseStaffId: undefined,
    inspectionStaffId: undefined,
    location: '',
    remark: '',
    state: 1,
    product: [],
    StaffPhone: '',
    salesmanName: '',
  });

  // 表格配置
  const args = reactive({
    stripe: false,
    rowSelection: {
      type: 'checkbox',
      showCheckedAll: true,
      onlyCurrent: false,
    },
    scroll: {
      y: 2000,
      x: 500,
    },
    bordered: {
      headerCell: true,
    },
    rowKey: 'id', // 批量选择按钮 以合同名称来作为key
  });

  let taskData = reactive<any>({
    form: {},
  });

  const getProduct = () => {
    try {
      return Promise.resolve(form.value.product);
    } catch (err: unknown) {
      console.log(err);
    }
  };

  const BuyOrderList = ref();
  const getBuyOrderList = async () => {
    const res = await getPurchaseOrderList({ page: 1, size: -1 });
    BuyOrderList.value = res.content;
  };
  getBuyOrderList();
  const isSelectSupplier = ref(false);

  const getInfo = async (id: number) => {
    const res = await getSinglePurchaseOrder({ id });
    form.value.purchaseStaffId = res.salesman;
    form.value.purchaseOrderNumber = res.number;
    form.value.purchaseStaffName = res.salesmanName;
    form.value.supplierId = res.supplierId;
    form.value.supplierName = res.supplierName;
    form.value.product = res.product;
    form.value.product.forEach((element) => {
      element.purchaseOrderProduct = {
        productId: element.id,
        entryWarehouseId: undefined,
        amount: undefined,
        level: undefined,
        detectionType: undefined,
      };
    });
    tableRef.value.loadData();
  };

  const getUserInfo = async (id: any) => {
    const res = await Getsingleperson({ id });
    form.value.StaffPhone = res.mobile;
  };

  const submit = async () => {
    form.value.purchaseOrderProduct = [];
    form.value.product.forEach((item) => {
      form.value.purchaseOrderProduct.push(item.purchaseOrderProduct);
    });
    await postWarehouseEntry({
      ...form.value,
    });
    router.back();
  };
</script>

<style scoped lang="less">
  .basic-item {
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid rgba(242, 243, 245, 0.852);
  }

  .title {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .title-item {
    width: 95%;
    height: 60px;
    margin: auto;
    background-color: rgba(245, 247, 250);
    line-height: 60px;
    margin-top: 20px;
  }

  .info-item {
    width: 100%;
  }

  .report-item {
    width: 95%;
    height: 60px;
    margin: auto;
    background-color: rgba(245, 247, 250);
    line-height: 60px;
  }

  .order-info {
    width: 95%;
    margin: 20px auto;
  }

  :deep(.arco-form) {
    width: 90%;
  }
</style>
